﻿
from core import uicomponents, db, qpath
from PyQt4 import QtGui, QtCore

abjad_names = []
abjad_value = {u"ا":1, u"آ":1, u"إ":1, u"أ":1, u"ب":2, u"ج":3, u"د":4, u"ه":5, u"و":6, u"ز":7, u"ح":8, u"ط":9, u"ي":10, u"ى":10, u"ک":20, u"ك":20, u"ل":30, u"م":40, u"ن":50, u"س":60, u"ع":70, u"ف":80, u"ص":90, u"ق":100, u"ر":200, u"ش":300, u"ت":400, u"ث":500, u"خ":600, u"ذ":700, u"ض":800, u"ظ":900, u"غ":1000}

def CalcWordAbjad(w):
	t = 0
	i = 0
	for c in w.word.decode("utf8"):
		if c in abjad_value.keys():
			t = t + abjad_value[c]
		i = i + 1	
	return t		

def CalcAbjad():
	w = uicomponents.SharedUi.selectionStartWord
	total_abjad = 0
	while w is not uicomponents.SharedUi.selectionEndWord:
		total_abjad = CalcWordAbjad(w) + total_abjad
		if w.n == len(w.aye.words):
			if w.aye.n == len(w.aye.sure.ayat):
				w = qpath.get("%d/" % (w.aye.sure.id+2)).ayat[0].words[0]
			else:
				w = w.aye.sure.ayat[w.aye.n].words[0]
		else:	
			w = w.aye.words[w.n]

	total_abjad = total_abjad + CalcWordAbjad(uicomponents.SharedUi.selectionEndWord)
	
	uicomponents.SharedUi.textDialog.showMe(u'ابجد کلمات انتخاب شده',"%d" % total_abjad,size=0)

CalcAbjadAction = QtGui.QAction(u'محاسبه ابجد', None)
uicomponents.SharedUi.wordPopupMenu.addAction(CalcAbjadAction)
uicomponents.SharedUi.ayePopupMenu.addAction(CalcAbjadAction)
uicomponents.SharedUi.selectionPopupMenu.addAction(CalcAbjadAction)

uicomponents.SharedUi.modulesListMenu.addAction(CalcAbjadAction)

QtCore.QObject.connect(CalcAbjadAction, QtCore.SIGNAL('triggered()'), CalcAbjad)

